using Digitalmes.Domain.Entities.Mes.Processes;

namespace Digitalmes.Application.Mes.Processes.Routings.Queries;

/// <summary>
/// 工艺路线分页查询命令。
/// </summary>
public sealed record GetRoutingListWithPagedQuery(
    long? LineId,
    long? ProductId,
    int PageNumber,
    int PageSize) : IPagedQuery, IRequest<IResult<PagedList<ProcRoutingDto>>>;

internal sealed class GetRoutingListWithPagedQueryHandler(IRepository<ProcRouting> repo) : IRequestHandler<GetRoutingListWithPagedQuery, IResult<PagedList<ProcRoutingDto>>>
{
    public async Task<IResult<PagedList<ProcRoutingDto>>> Handle(GetRoutingListWithPagedQuery request, CancellationToken cancellationToken)
    {
        // 分页查询后再单独查询，查看是否有更简化的方法
        var items = await repo.AsQueryable()
            .WhereIF(request.LineId > 0, m => m.LineId == request.LineId)
            .WhereIF(request.ProductId > 0, m => m.ProductId == request.ProductId)
            .OrderBy(m => m.Order)
            .GetPagedListAsync(request.PageNumber, request.PageSize, cancellationToken);
        return Result.From(items.Adapt<PagedList<ProcRoutingDto>>());
    }
}
